<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 专辑
 *
 */
class Album extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		//加载usermodel
		$this->load->model('usermodel');
		$this->uid = $this->usermodel->get_id_by_token($this->input->post('token'));
		$this->load->model('albummodel');
	}
	
	function index()
	{
		$this->myalbums();
	}
	
	/**
	 * 我的专辑列表
	 *
	 */
	function myalbums()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$albums = $this->_get_albums($this->uid);
		if(count($albums) > 0){
			//输出最终的xml
			echo $this->xmllib->get_xml($albums, array('album'), 'albums');
			exit;
		}else{
			//结果集为空
			echo $this->xmllib->empty_xml();
			exit;
		}
	}
	
	/**
	 * 查看别人的专辑列
	 *
	 */
	function other_myalbums()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$uid = $this->input->post('uid');
		$albums = $this->_get_albums($uid);
		if(count($albums) > 0){
			//输出最终的xml
			echo $this->xmllib->get_xml($albums, array('album'), 'albums');
			exit;
		}else{
			//结果集为空
			echo $this->xmllib->empty_xml();
			exit;
		}
	}
	
	/**
	 * 添加专辑
	 *
	 */
	function addalbum()
	{
		//加载表单验证类库
  		$this->load->library('form_validation');
  		
		header('Content-type:text/xml');// 向浏览器输出 xml
		$_POST['name'] = isset($_POST['name']) ? $_POST['name'] : '';//此处设置时为了防止生成xml报错
		
		$this->form_validation->set_rules('name', '专辑名称', 'trim|required|min_length[2]|max_length[16]|xss_clean');
		$this->form_validation->set_message('required', '%s 不能为空');
		$this->form_validation->set_message('min_length', '%s 字数不能小于%d');
		$this->form_validation->set_message('max_length', '%s 字数不能大于%d');
		
		$this->form_validation->set_error_delimiters('', '');
		if ($this->form_validation->run() == FALSE){
			$name_error = form_error('name');
			echo $this->xmllib->error_xml($name_error);
			exit;
		}
		
		$name = $this->input->post('name');
		if($this->albummodel->check_album_name($this->uid, $name)){
			//会员自己已经创建过同名的专辑名
			echo $this->xmllib->error_xml('此专辑已经存在，请选择其他专辑名称');
			exit;
		}
		
		//执行添加
		if($this->albummodel->insert_album(array('uid' => $this->uid, 'name' => $name))){
			//添加成功
			$row = $this->albummodel->get_album_by_name($this->uid, $name);
			$albumid = $row['id'];
			//需要添加动态
			$this->load->model('activemodel');
			$this->activemodel->insert_active(array('uid' 			=> $this->uid,
													'active_id'		=> $albumid,
													'active_type'	=> ALBUM_ACTIVE,
													'memo'			=> '添加了'));
			echo $this->xmllib->success_xml('添加专辑成功');
			exit;
		}else{
			//添加失败
			echo $this->xmllib->error_xml('添加专辑失败，请稍后再试');
			exit;
		}
	}
	
	/**
	 * 给专辑添加应用成员
	 *
	 */
	function addapps()
	{
		//加载表单验证类库
  		$this->load->library('form_validation');
  		
		header('Content-type:text/xml');// 向浏览器输出 xml
		$_POST['appid_list'] = isset($_POST['appid_list']) ? $_POST['appid_list'] : '';//此处设置时为了防止生成xml报错
		
		$this->form_validation->set_rules('appid_list', '', 'trim|required');
		$this->form_validation->set_message('required', '没有为专辑选择应用');
		
		$this->form_validation->set_error_delimiters('', '');
		if ($this->form_validation->run() == FALSE){
			$appid_list_error = form_error('appid_list');
			echo $this->xmllib->error_xml($appid_list_error);
			exit;
		}
		
		$albumid = $this->input->post('albumid');
		if(empty($albumid)) {
			echo $this->xmllib->error_xml('没有指定专辑');
			exit;
		}
		
		if(!$this->albummodel->check_album($albumid, $this->uid)){
			//要添加应用的专辑不是当前用户自己的
			echo $this->xmllib->error_xml('此专辑不属于您，无法操作');
			exit;
		}
		$appid_list = $this->input->post('appid_list');
		$appid_arr = explode(',', trim($appid_list, ','));
		if(count($appid_arr) > 0){
			//加载appmodel
			$this->load->model('appmodel');
			foreach ($appid_arr as $appid) {
				if(empty($appid)) continue;
				//检测要添加的应用是否是当前用户自己的
				if(!$this->appmodel->check_app($appid, $this->uid)) continue;
				//如果此专辑中已经存在了此应用，则跳过
				if($this->albummodel->check_albumapp($albumid, $appid)) continue;
				//添加
				$this->albummodel->insert_albumapp($albumid, $appid);
			}
			//添加成功
			echo $this->xmllib->success_xml('添加专辑应用成功');
			exit;
		}else{
			//添加失败
			echo $this->xmllib->error_xml('为专辑添加应用失败，请稍后再试');
			exit;
		}
	}
	
	/**
	 * 删除专辑
	 *
	 */
	function delalbum()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$albumid = $this->input->post('albumid');
		if($this->albummodel->check_album($albumid, $this->uid)){
			//要删除的专辑属于当前用户的
			$this->albummodel->delete_album($albumid);
			//删除成功
			//需要添加动态
			$this->load->model('activemodel');
			$this->activemodel->insert_active(array('uid' 			=> $this->uid,
													'active_id'		=> $albumid,
													'active_type'	=> ALBUM_ACTIVE,
													'memo'			=> '删除了'));
			echo $this->xmllib->success_xml('删除专辑应用成功');
			exit;
		}else{
			echo $this->xmllib->error_xml('删除专辑失败，请稍候再试');
			exit;
		}
	}
	
	/**
	 * 分享专辑
	 *
	 */
	function sharealbum()
	{
		//加载表单验证类库
  		$this->load->library('form_validation');
  		
		header('Content-type:text/xml');// 向浏览器输出 xml
		$_POST['albumid_list'] = isset($_POST['albumid_list']) ? $_POST['albumid_list'] : '';//此处设置时为了防止生成xml报错
		
		//_list 均是由半角逗号分隔开的
		$this->form_validation->set_rules('albumid_list', '要分享的专辑', 'trim|required|xss_clean');
		//$this->form_validation->set_rules('uid_list', '', 'trim|xss_clean');
		$this->form_validation->set_rules('content', '点评内容', 'trim|max_length[140]|xss_clean');
		$this->form_validation->set_message('required', '请选择%s');
		$this->form_validation->set_message('max_length', '%s字数不要超过 %d');
		
		$this->form_validation->set_error_delimiters('', '');
		if ($this->form_validation->run() == FALSE){
			$albumid_list_error = form_error('albumid_list');
			echo $this->xmllib->error_xml($albumid_list_error);
			exit;
		}
		
		$albumid_arr = explode(',', trim($this->input->post('albumid_list'), ','));
		//过滤数据
		//$albumid_arr = array_flip($albumid_arr);
		//$albumid_arr = array_flip($albumid_arr);
		$albumid_arr = array_unique($albumid_arr);
		
		//分享理由（点评内容）
		$content = $this->input->post('content');
		
		//检测提交的应用是不是属于当前用户自己所有的
		//加载comment model
		$this->load->model('commentmodel');
		$this->load->model('activemodel');
		foreach ($albumid_arr as $k=>$albumid){
			if(empty($albumid) || !$this->albummodel->check_album($albumid, $this->uid) || $this->albummodel->check_album_shared($albumid)){ 
				unset($albumid_arr[$k]);//去除无效的
				continue;
			}
			//设置应用的状态为 以分享 
			$this->albummodel->set_album_shared($albumid);
			//将点评插入点评表中
			$content !== '' && $this->commentmodel->insert_comment(array('comment_type'		=> ALBUM_COMMENT,
																		'comment_id'		=> $albumid,
																		'uid'				=> $this->uid,
																		'content'			=> $content,
																		'share_or_comment'	=> IS_SHARE));
			//添加动态
			$this->activemodel->insert_active(array('uid' 			=> $this->uid,
													'active_id'		=> $albumid,
													'active_type'	=> ALBUM_ACTIVE,
													'memo'			=> '分享了'));
		}
		
		if(count($albumid_arr) == 0){
			//没有有效的可分享的应用
			echo $this->xmllib->error_xml('要分享的专辑无效或已被分享，请重新设置');
			exit;
		}

		//分享成功提示
		echo $this->xmllib->success_xml('您的专辑分享成功');
		exit;
	}
	
	/**
	 * 评论专辑
	 *
	 */
	function commentalbum()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		//加载表单验证类库
  		$this->load->library('form_validation');
		$_POST['albumid'] = isset($_POST['albumid']) ? $_POST['albumid'] : '';//此处设置时为了防止生成xml报错
		$this->form_validation->set_rules('albumid', '要点评的专辑', 'required');
		$this->form_validation->set_rules('content', '点评内容', 'trim|required|max_length[140]|xss_clean');
		$this->form_validation->set_message('required', '%s不能为空');
		$this->form_validation->set_message('max_length', '%s字数不要超过 %d');
		$this->form_validation->set_error_delimiters('', '');
		if ($this->form_validation->run() == FALSE){
			//错误提示
			$albumid_error = form_error('albumid');
			$content_error = form_error('content');
			if($albumid_error)
				echo $this->xmllib->error_xml($albumid_error);
			elseif ($content_error)
				echo $this->xmllib->error_xml($content_error);
			exit;
		}else{
			//验证通过
			$albumid = $this->input->post('albumid');
			$content = $this->input->post('content');
			//查询要评论的应用是否存在
			$this->load->model('albummodel');
			if(!$this->albummodel->check_album($albumid)){
				//此专辑不存在
				echo $this->xmllib->error_xml('您要评论的专辑不存在');
				exit;
			}
			//添加评论信息
			$this->load->model('commentmodel');
			$data = array('comment_id' 		=> $albumid, 
						  'comment_type'	=> ALBUM_COMMENT,
						  'uid' 			=> $this->uid, 
						  'content' 		=> $content,
						  'share_or_comment'=> IS_COMMENT);
			if($this->commentmodel->insert_comment($data)){
				//需要添加动态
				$this->load->model('activemodel');
				$this->activemodel->insert_active(array('uid' 			=> $this->uid,
														'active_id'		=> $albumid,
														'active_type'	=> ALBUM_ACTIVE,
														'memo'			=> '评论了'));
				echo $this->xmllib->success_xml('评论添加成功');
				exit;
			}else{
				echo $this->xmllib->error_xml('评论失败，请稍候再试');
				exit;
			}
		}
	}
	
	/**
	 * 取得专辑中的应用软件
	 *
	 */
	function albumapps()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$albumid = $this->input->post('albumid');
		$apps = $this->albummodel->get_album_apps($albumid);
		if($apps){
			echo $this->xmllib->get_xml($apps, array('app'), 'apps');
			exit;
		}else{
			//为空
			echo $this->xmllib->empty_xml();
			exit;
		}
	}
	
	//获取我的 专辑，包括软件、评论、收藏数量
	function _get_albums($uid='')
    {
    	$this->dblib->set_query_limit();
    	/*$this->db->select('a.*, count(*) as appcount')
    				->from('album as a')
    				->join('albumapp as aa', 'a.id=aa.albumid', 'left')
    				->where(array('a.uid' => $uid, 'isdel' => 0))
    				->group_by('a.id')
    				->order_by('a.createtime', 'desc');*/
	$this->db->select('a.*')
			->from('album as a')
			->where(array('a.uid' => $uid, 'isdel' => 0))
			->group_by('a.id')
			->order_by('a.createtime', 'desc');
    	$query = $this->db->get();
    	$result = $query->result_array();
    	foreach ($result as $k=>$v){
		//应用
		$this->db->select('count(*) as appcount')
    				 ->from('albumapp')
    				 ->where(array('albumid' => $v['id']));
    		$query = $this->db->get();
    		$res = $query->row_array();
    		$result[$k]['appcount'] = $res['appcount'];
    		unset($res);
    		//评论
    		$this->db->select('count(*) as commentscount')
    				 ->from('comments')
    				 ->where(array('uid' => $uid, 'comment_type' => ALBUM_COMMENT));
    		$query = $this->db->get();
    		$res = $query->row_array();
    		$result[$k]['commentscount'] = $res['commentscount'];
    		unset($res);
    		
    		//收藏
    		$this->db->select('count(*) as collectioncount')
    				 ->from('collection')
    				 ->where(array('uid' => $uid, 'collection_type' => ALBUM_COLLECTION));
    		$query = $this->db->get();
    		$res = $query->row_array();
    		$result[$k]['collectioncount'] = $res['collectioncount'];
    		unset($res);
    	}
    	return $result;
    }
	
}

/* End of file album.php */
/* Location: ./application/controllers/album.php */